<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $userId              = "";
  $month               = "";
  $target              = "";
  $achievedTarget      = "";
  $userName            = "";
  $userTargetArray     = array();
  $totalTarget         = 0;
  $totalAchievedTarget = 0;
  $totalBalanceTarget  = 0;
  $userIdSearch        = array();
  $userNameSearch      = array();
  // After Post : Start
  if(isset($_POST['userId']))
  {
  	$month = $_POST['monthYear']."-".$_POST['monthMonth']."-01";
    $selectUser = "SELECT userId,month,target,achievedTarget
  	                 FROM usertarget
  	                WHERE userId = ".$_POST['userId']." 
	                    AND month  = '".$month."' ";
	  $selectUserQuery = mysql_query($selectUser);
	  if($selectUserRes = mysql_fetch_array($selectUserQuery))
	  {
	    $UpdateUserTargate = "UPDATE usertarget
	                             SET target = ".$_POST['target']." + ".$selectUserRes['target'].",
	                                 achievedTarget = ".$_POST['achievedTarget']." + ".$selectUserRes['achievedTarget']." 
	                           WHERE userId = ".$_POST['userId']."
	                             AND month = '".$month."'    ";
	    $UpdateUserTargateQuery = mysql_query($UpdateUserTargate);
	    
	    //To store date in usertargetachieved table : Starts
	    $insertAchievedTarget = "INSERT INTO usertargetachieved(userTargetId, orderValue, orderDate)
	                                   VALUE ('".$_POST['userId']."','".$_POST['achievedTarget']."','".date('Y-m-d')."')";
      $insertAchievedTargetRes = mysql_query($insertAchievedTarget);	
      //To store date in usertargetachieved table : Ends
	  }
	  else
	  {
      $insertUser = "INSERT INTO usertarget (userId,month,target,achievedTarget)
                           VALUE ('".$_POST['userId']."','".$month."','".$_POST['target']."',".$_POST['achievedTarget'].")";
      $insertUserResult = mysql_query($insertUser);
      if(!$insertUserResult)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:userTarget.php");
    }
  }
      
  // After Post : End

  $selectUser = "SELECT userId,userName
                   FROM user
                  WHERE userType IN('superAdmin','marketingManager','marketingExecutive')
               ORDER BY userName";
  $selectUserResult = mysql_query($selectUser);
  $b = 0;
  while($userRow = mysql_fetch_array($selectUserResult))
  {
    $userId[$b]   = $userRow['userId'];
    $userName[$b] = $userRow['userName'];
    $b++;
  }
  // userId for search combo//
  $selectUser = "SELECT DISTINCT user.userId,user.userName
                   FROM user
                   JOIN usertarget ON usertarget.userId = user.userId
                  WHERE user.userType IN('superAdmin','marketingManager','marketingExecutive')
               ORDER BY userName";
  $selectUserResult = mysql_query($selectUser);
  $a = 0;
  while($userRow = mysql_fetch_array($selectUserResult))
  {
    $userIdSearch[$a]   = $userRow['userId'];
    $userNameSearch[$a] = $userRow['userName'];
    $a++;
  }
  // userId for search combo END//
    
  $targetCount = 0;
  $selectTarget = "SELECT usertarget.usertargetId,user.userId,user.userName,DATE_FORMAT(usertarget.month,'%b-%Y')AS month,
                          usertarget.target,usertarget.achievedTarget
                     FROM usertarget
                     JOIN user
                    WHERE usertarget.userId = user.userId";
  $selectTargetResult = mysql_query($selectTarget);
  while($targetRow = mysql_fetch_array($selectTargetResult))
  {
    $userTargetArray[$targetCount]['usertargetId']   = $targetRow['usertargetId'];
    $userTargetArray[$targetCount]['userName']       = $targetRow['userName'];
    $userTargetArray[$targetCount]['month']          = $targetRow['month'];
    $userTargetArray[$targetCount]['target']         = $targetRow['target'];
    $userTargetArray[$targetCount]['achievedTarget'] = $targetRow['achievedTarget'];
    $userTargetArray[$targetCount]['balanceTarget']  = $targetRow['target'] - $targetRow['achievedTarget'];
    $totalTarget         += $targetRow['target'];
    $totalAchievedTarget += $targetRow['achievedTarget'];
    $totalBalanceTarget  += $targetRow['target'] - $targetRow['achievedTarget'];
    $targetCount++;
  }
  
  include("./bottom.php");
  $smarty->assign("userId",$userId);
  $smarty->assign("month",$month);
  $smarty->assign("target",$target);
  $smarty->assign("achievedTarget",$achievedTarget);
  $smarty->assign("userName",$userName);
  $smarty->assign("userIdSearch",$userIdSearch);
  $smarty->assign("userNameSearch",$userNameSearch);
  $smarty->assign("userTargetArray",$userTargetArray);
  $smarty->assign("totalTarget",$totalTarget);
  $smarty->assign("totalAchievedTarget",$totalAchievedTarget);
  $smarty->assign("totalBalanceTarget",$totalBalanceTarget);
  $smarty->display("userTarget.tpl");
}
?>